package com.nd.android.u.ui.widge.lift;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.RelativeLayout;

public class Lift {
	private Context mContext;
	private RelativeLayout mRelativeLayout;
	private int mWidth;           
	private int mHeight;
	private int mFlag = 1;        //用来判断是否动画在运动，判断是否二次点击
	private PetalView mPetalView = null;
	private final int VIEW_DISAPPER = 1;        //动画视图的消失标志־
	private final int REMOVE_VIEW = 2;          //动画视图的移除标志־
	private final static int SLEEP_TIME = 4000;  //动画运动时间
	private boolean hasMeasured=false;
	private BirthThread mBirthThread=null;
	private RemoveThread mRemoveThread=null;
	private final static int WAIT_VIEW=3;
	private boolean isFall;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == VIEW_DISAPPER) {       //当动画运动4秒后不再产生鲜花粒子
				if(mPetalView.mPetalThread!=null){
			    	mPetalView.mPetalThread.num *= 1000;
				    mRemoveThread=new RemoveThread();
				    mRemoveThread.start();
				}
			} else if (msg.what == REMOVE_VIEW) {      //从RelativeLayout中移除动画
				if(mPetalView!=null){
				  mRelativeLayout.removeView(mPetalView);    
				  mFlag = 1;
				}
			}else if(msg.what==WAIT_VIEW){
				  fallLift(isFall);
			}
		}
	};
	
	public Lift(Context context,RelativeLayout relativeLayout) {
            mContext=context;
            mRelativeLayout=relativeLayout;
            ViewTreeObserver vto=mRelativeLayout.getViewTreeObserver();
    		vto.addOnPreDrawListener(new OnPreDrawListener() {
    			
    			@Override
    			public boolean onPreDraw() {
    				if(hasMeasured==false){
    				  mWidth = mRelativeLayout.getWidth(); //屏幕宽度（像素）
    				  mHeight = mRelativeLayout.getHeight(); // 屏幕高度（像素）
    				  hasMeasured=true;
    				}
    				return true;
    			}
    		});
	}

	/**
	 * 掉礼物方法
	 */
	public void fallLift(boolean isSelection){      //selection   对送蛋糕还是送鲜花的选择。    0  是蛋糕      1是鲜花
		isFall=isSelection;
		if(mWidth==0&&(mHeight==0)){
			new WaitDrawThread().start();
		}else{
			getLift(isFall);
		}		
	}

	/**
	 * 控制礼物运动时间的线程
	 */
	class BirthThread extends Thread {
		int mTime;
		public BirthThread(int time) {
             mTime=time;
		}
		@Override
		public void run() {
			try {
				Thread.sleep(mTime);
				Message msg = new Message();
				msg.what = VIEW_DISAPPER;
				handler.sendMessage(msg);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 控制最后一个礼物掉落显示屏所需的时间
	 */
	class RemoveThread extends Thread {

		@Override
		public void run() {
			try {
				Thread.sleep(SLEEP_TIME*2);
				Message msg = new Message();
				msg.what = REMOVE_VIEW;
				handler.sendMessage(msg);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	class WaitDrawThread extends Thread{
		@Override
		public void run() {
			try {
				Thread.sleep(100);
				Message msg = new Message();
				msg.what = WAIT_VIEW;
				handler.sendMessage(msg);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
	}

	public void getLift(boolean isSelection){
		if (null == mPetalView) {
			mPetalView = new PetalView(mContext, mWidth, mHeight,isSelection);
		}
		if (mFlag == 1) {    //第一次触发掉礼物
			mFlag = 0;
			mPetalView = new PetalView(mContext, mWidth, mHeight,isSelection);
			mRelativeLayout.addView(mPetalView);
			mBirthThread=new BirthThread(SLEEP_TIME);
			mBirthThread.start();
		}else{                      //当礼物还没掉完，又有包含掉礼物的key出现，触发掉礼物
			if(mBirthThread!=null){
				mBirthThread.interrupt();
			}
			if(mRemoveThread!=null){
				mRemoveThread.interrupt();
			}
			
			mRelativeLayout.removeView(mPetalView);    //移除还在运动的掉礼物动画，重新加载
			mFlag = 0;
			mPetalView = new PetalView(mContext, mWidth, mHeight,isSelection);
			mRelativeLayout.addView(mPetalView);
			mBirthThread=new BirthThread(SLEEP_TIME);
			mBirthThread.start();
		}
	}
}
